<?php
/**
 * 验证码处理
 */
namespace Api\Controller;

use Common\Controller\AppserviceController;

class SwimerController extends AppserviceController {

	//注册验证码
	public function yzm(){
		$config =array(
				'fontSize'    =>    26,    // 验证码字体大小
				'length'      =>    4,     // 验证码位数
				'useNoise'    =>    false, // 关闭验证码杂点
				'useCurve'    =>    false,
		);
		$Verify =     new \Think\Verify($config);
		$Verify->entry();
	}
	public function check_verify($code, $id = ''){
		$verify = new \Think\Verify();
		return $verify->check($code, $id);
	}


    public function menulist() {
    	$record = array ('status' => "10000",'msg' => '抱歉，没有找到相关信息！');
    	$request=I('request.');
    	$record = $this->checkMember($request);
    	if (!empty($record['data']['status'])) {
    		$ident = $request['ident'];
    		$cid = $request['cid'];
    		if($ident){
    			$nav = M("Nav")->field("id")->where(array("ident"=>$ident,"cid"=>$cid))->find();
    			if(!empty($nav)){
    				$navlist = M("Nav")
    				->field("id,label,icon,listorder")
    				->where(array("cid"=>$cid,"parentid"=>$nav['id']))
					->order("listorder ASC,id DESC")
					->limit(0, $request['num'])
    				->select();
    				$urlIn = 'http://' . $_SERVER['HTTP_HOST'].__APP__;
    				$url = str_replace("index.php","",$urlIn);
    				$urlUp = $url."data/upload/";
    				if(!empty($navlist)){
    					foreach ($navlist as $key=>$val){
    						if(!empty($val['icon'])){
    							$navlist[$key]['icon'] = $urlUp.$val['icon'];
    						}
    					}
    					$record = array ('status' => "10001",'data' => $navlist);
    				}else{
    					$record = array ('status' => "10000",'msg' => '抱歉，没有找到相关信息！');
    				}
    			}else{
    				$record = array ('status' => "10000",'msg' => '抱歉，没有找到相关信息！');
    			}
    		}else{
    			$record = array ('status' => "10000",'msg' => '抱歉，没有找到相关信息！');
    		}
    	}
    	$this->json($record);
    }
    
    public function natalist() {
    	$record = array ('status' => "10000",'msg' => '抱歉，没有找到相关信息！');
    	$request=I('request.');
    	$lat = $request['lat'];
    	$log = $request['log'];
    	$name = $request['keyword'];
		$page = !empty($request['p'])?$request['p']:1;
    	if(!empty($name)){
    		$where['subject']=array('like',"%".$name."%");
    		$where['content_addr']=array('like',"%".$name."%");
    		$where["_logic"]         ="OR";
    	}
    	if(!empty($lat) && !empty($log)){
    		$mem_addr = $this->getAddress($lat, $log);
    		$where['content_addr']=array('like',"%".$mem_addr['city']."%");
    	}
    	
    	$num = !empty($request['num'])?$request['num']:10;
		$pp = ($page-1)*$num;
		$limit = "$pp,{$num}";
    	
    	$nata = M("natatoria")
    	->field("id,subject,content_addr,thumbsimg,series,price,long1")
    	->where($where)
    	->order("series ASC,id DESC")
    	->limit($limit)
    	->select();
    	$urlIn = 'http://' . $_SERVER['HTTP_HOST'].__APP__;
    	$url = str_replace("index.php","",$urlIn);
    	$urlUp = $url."data/upload/";
    	if(!empty($nata)){
    		foreach ($nata as $key=>$val){
    			if(!empty($val['thumbsimg'])){
    				$nata[$key]['thumbsimg'] = $urlUp.$val['thumbsimg'];
    			}
    		}
    		$record = array ('status' => "10001",'data' => $nata);
    	}else{
    		$record = array ('status' => "10001",'data' => $nata);
    	}
    	$this->json($record);
    }
    
    public function natahotlist() {
    	$record = array ('status' => "10000",'msg' => '抱歉，没有找到相关信息！');
    	$request=I('request.');
    	$where['is_hot']="1";
		$page = !empty($request['p'])?$request['p']:1;
    	$num = !empty($request['num'])?$request['num']:10;
		$pp = ($page-1)*$num;
		$limit = "$pp,{$num}";
    	
    	$nata = M("natatoria")
    	->field("id,subject,content_addr,thumbsimg,series,price,long1")
    	->where($where)
    	->order("series ASC,id DESC")
    	->limit($limit)
    	->select();
    	$urlIn = 'http://' . $_SERVER['HTTP_HOST'].__APP__;
    	$url = str_replace("index.php","",$urlIn);
    	$urlUp = $url."data/upload/";
    	if(!empty($nata)){
    		foreach ($nata as $key=>$val){
    			if(!empty($val['thumbsimg'])){
    				$nata[$key]['thumbsimg'] = $urlUp.$val['thumbsimg'];
    			}
    		}
    		$record = array ('status' => "10001",'data' => $nata);
    	}else{
    		$record = array ('status' => "10001",'data' => $nata);
    	}	
    	$this->json($record);
    }
    
    public function nataview() {
    	$record = array ('status' => "10000",'msg' => '抱歉，没有找到相关信息！');
    	$request=I('request.');
	    $nataid = $request['nataid'];
	    $nata = M("natatoria")
	    ->field("subject,content_addr,thumbimg,price,long1,tel,body")
	    ->where(array("id"=>$nataid))
	    ->find();
	    $urlIn = 'http://' . $_SERVER['HTTP_HOST'].__APP__;
	    $url = str_replace("index.php","",$urlIn);
	    $urlUp = $url."data/upload/";
	    if(!empty($nata)){
	    	if(!empty($nata['thumbimg'])){
	    		$arr = explode(",", $nata['thumbimg']);unset($nata['thumbimg']);
	    		if(!empty($arr) && is_array($arr)){
	    			foreach ($arr as $key=>$val){
	    				$nata['thumbimgs'][$key] = $urlUp.$val;
	    			}
	    		}
	    	}
            $nata['body_text'] = $nata['body'];
	    	$nata['body'] = 'http://' . $_SERVER['HTTP_HOST'].U('Api/Swimer/nataview_body',array("nataid"=>$nataid));
	    	$record = array ('status' => "10001",'data' => $nata);
	    }else{
	    	$record = array ('status' => "10001",'data' => $nata);
	    }	
    	$this->json($record);
    }
    
    public function nataview_body() {
    	$nataid = I('nataid');
    	$item = M("natatoria")
    	->field("body")
    	->where(array("id"=>$nataid))
    	->find();
    	if(!empty($item['body'])){
    		$existdata=true;
    	}else{
    		$existdata=false;
    	}
    	$this->assign("content",$item['body']);
    	$this->assign("existdata", $existdata);
    	$this->display(':body');
    }
    
    public function videolist(){
    	$record = array ('status' => "10000",'msg' => '抱歉，没有找到相关信息！');
    	$request=I('request.');
		$page = !empty($request['p'])?$request['p']:1;
	    $num = !empty($request['num'])?$request['num']:10;
		$pp = ($page-1)*$num;
		$limit = "$pp,{$num}";
	    $active = M("swimShow")
	    ->field("id,subject,thumbimg,series,create_time,video,video_time")
	    ->where(array("status"=>"1"))
	    ->order("series ASC,create_time DESC")
	    ->limit($limit)
	    ->select();
	    $urlIn = 'http://' . $_SERVER['HTTP_HOST'].__APP__;
	    $url = str_replace("index.php","",$urlIn);
	    $urlUp = $url."data/upload/";
	    if(!empty($active) && is_array($active)){
	    	if(!empty($active) && is_array($active)){
	    		foreach ($active as $key=>$val){
	    			if(!empty($val['thumbimg'])){
	    				$active[$key]['thumbimg'] = $urlUp.$val['thumbimg'];
	    			}
	    			if(!empty($val['video'])){
	    				$active[$key]['video'] = $urlUp.$val['video'];
	    			}
	    		}
	    	}
	    	$record = array ('status' => "10001",'data' => $active);
	    }else{
	    	$record = array ('status' => "10001",'data' => $active);
	    }	
    	$this->json($record);
    }
    public function videoview(){
    	$record = array ('status' => "10000",'msg' => '抱歉，没有找到相关信息！');
    	$request=I('request.');
	    $videoid = I("videoid");
	    $item = M("swimShow")
	    ->field("subject,thumbimg,review,follow,create_time,source,body,video,matchid")
	    ->where(array("status"=>"1","id"=>$videoid))
	    ->find();
	    $urlIn = 'http://' . $_SERVER['HTTP_HOST'].__APP__;
	    $url = str_replace("index.php","",$urlIn);
	    $urlUp = $url."data/upload/";
	    if(!empty($item) && is_array($item)){
	    	if(!empty($item['video'])){
	    		if(!empty($item['thumbimg'])){
	    			$item['thumbimg'] = $urlUp.$item['thumbimg'];
	    		}
	    		if(!empty($item['video'])){
	    			$item['video'] = $urlUp.$item['video'];
	    		}
	    		$item['create_time'] = date("Y-m-d",strtotime($item['create_time']));
                $item['body_text'] = $item['body'];
	    		$item['body'] = 'http://' . $_SERVER['HTTP_HOST'].U('Api/Swimer/videoview_body',array("videoid"=>$videoid));
	    	}
	    	$record = array ('status' => "10001",'data' => $item);
	    }else{
	    	$record = array ('status' => "10001",'data' => $item);
	    }	
    	$this->json($record);
    }
    
    public function videoview_body(){
    	$videoid = I("videoid");
    	$item = M("swimShow")
    	->field("body")
    	->where(array("status"=>"1","id"=>$videoid))
    	->find();
		if(!empty($item['body'])){
			$existdata=true;
		}else{
			$existdata=false;
		}
		$this->assign("content",$item['body']);
		$this->assign("existdata", $existdata);
		$this->display(':body');
    }
    
    //json返回值
    function json($arr){
    	if ($arr['status']=='10001') {
			if (!empty($arr['msg'])) {
				$arr1['status'] = $arr['status'];
				$arr1['msg'] = $arr['msg'];
			}else{
				$arr1['status'] = $arr['status'];
				$arr1['result']['data'] = $arr['data'];
			}
			exit(json_encode($arr1));
		}
		if ($arr['status']!='10001') {
			if (!empty($arr['msg'])) {
				$arr1['status'] = $arr['status'];
				$arr1['msg'] = $arr['msg'];
			}
			exit(json_encode($arr1));
		}
    }


}